#include "widget.h"
#include <QLabel>
#include <QDateTime>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *hBoxLayout;
    QVBoxLayout *mainLayout = new QVBoxLayout();
    setLayout(mainLayout);

    hBoxLayout = new QHBoxLayout();
    mainLayout->addLayout(hBoxLayout);

    secondLabel = new QLabel();
    QFont font = secondLabel->font();
    font.setBold(true);
    font.setPixelSize(32);
    secondLabel->setFont(font);
    hBoxLayout->addStretch();
    hBoxLayout->addWidget(secondLabel);
    hBoxLayout->addStretch();

    QGridLayout *gridLayout = new QGridLayout();
    mainLayout->addLayout(gridLayout);

    for(int i=0; i<10; i++) {
        gridLayout->addWidget(new QLabel(QString::number(i)), 0, i+1, 1, 1, Qt::AlignCenter);
        gridLayout->addWidget(new QLabel(QString::number(i)), i+1, 0, 1, 1, Qt::AlignCenter);
        for(int j=0; j<10; j++) {
            led[i][j] = new Led();
            gridLayout->addWidget(led[i][j], i+1, j+1, 1, 1, Qt::AlignCenter);
        }
        gridLayout->addWidget(new QLabel(QString::number(i)), 11, i+1, 1, 1, Qt::AlignCenter);
        gridLayout->addWidget(new QLabel(QString::number(i)), i+1, 11, 1, 1, Qt::AlignCenter);
    }
    timer = new QTimer();
    timer->setSingleShot(false);
    connect(timer, &QTimer::timeout, this, &Widget::_onTimerTimeout);
    timer->start(10);
}

Widget::~Widget()
{
}

void Widget::_onTimerTimeout()
{
    qint64 value = QDateTime::currentDateTime().toMSecsSinceEpoch();
    secondLabel->setText(QString::number(value));
    int temp = value % 1000;
    int row = temp / 100;
    int col = ((temp % 100)+5) / 10;
    int i, j;
    for(i=0; i<row; i++) {
        for(j=0; j<10; j++) {
            led[i][j]->lightOn();
        }
    }

    for(j=0; j<col; j++) {
        led[i][j]->lightOn();
    }
    while(i<10) {
        while(j < 10) {
            led[i][j++]->lightOff();
        }
        j=0;
        i++;
    }
}

